<?php


namespace App\Models\Tool;


use Illuminate\Support\Facades\File;

/**创建添加控制器类
 * Class MakeAddHtml
 * @package App\Models\Tool
 */
class MakeControllerT
{

    static function make($html_file,$xxxController,$controller_path,$views_path,$inputdata){
        /*
                [
                    "fied" => "title"
            "type" => "input"
            "title" => "权限节点名称"
            "checktype" => null
          ]*/

        $middhtml = [];
        $table = $inputdata['table'];
        $middhtml = join("\n", $middhtml);

        $controller_path=str_replace('/','\\',$controller_path);
        $controller_path = 'App\\'.substr($controller_path, 0, strlen($controller_path) - 1);
        $views_path=str_replace('views/','',$views_path);
        $views_path=str_replace('/','.',$views_path);



        $joins_fun =self::wirteJoinList($inputdata);
        if(!$joins_fun){
            $joins_fun = '';
        }



        $makecontent=<<<text
<?php

namespace {$controller_path};

use App\Http\Controllers\admin\defaults\BaseController;
use App\Helpers\commonFuc;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class $xxxController extends BaseController
{
    private \$tpviewhead='{$views_path}';
    private \$table='{$table}';
    function __construct(Request \$thisRequest)
    {
        parent::__construct(\$thisRequest,\$this->tpviewhead,\$this->table);
    }

    {$joins_fun}




}
text;
        $html_file=app_path($html_file);
        // 写入文件，存在的话覆盖写入
        File::replace(($html_file), $makecontent);
    }


    /**创建lists控制器  有关联表存在时
     * @param $inputdata
     * @return string
     */
    private static function wirteJoinList($inputdata){
        //dd($inputdata['lists']['list']);
        $fieds = [];
        $where_fieds = [];
        $where_inputs = [];
        foreach ($inputdata['lists']['list'] as $fied){
            if(strpos($fied['fied'],'.')){
                $where_fied = $fied['fied'];
                $where_fied_var = str_replace('.', '_', $fied['fied']);
                $fieds[] = $fied['fied'].' as '.$where_fied_var;
            }else{
                $where_fied = $inputdata['table'].'.'.$fied['fied'];
                $where_fied_var = $fied['fied'];
                $fieds[] = $inputdata['table'].'.'.$where_fied_var;
            }

            if($fied['so']){
                $where_inputs[]="\${$where_fied_var}=\$this->thisRequest->get('{$where_fied_var}');";
                $where_fieds[] = "if(\${$where_fied_var}){ \n           \$data =\$data->where('{$where_fied}', 'like', '%'.\${$where_fied_var}.'%');   \n        }";
            }
        }
        //这里是要显示的字段
        $fieds = join("','", $fieds);
        //这里是where 条件
        $where_fieds = join("\n        ", $where_fieds);
        $where_inputs = join("\n        ", $where_inputs);


        $function_lists = '';
        if(isset($inputdata['join'])){


            $jointablestring = [];
            foreach ($inputdata['join'] as $jointable){
                $jointable['fied_old'] = $inputdata['table'] .'.'.$jointable['fied_old'];
                switch ($jointable['join']){

                    case "Inner":
                        $jointablestring[] = "->join('{$jointable['table']}','{$jointable['fied']}','=','{$jointable['fied_old']}')";
                        break;
                    case "Left":
                        $jointablestring[] = "->leftJoin('{$jointable['table']}','{$jointable['fied']}','=','{$jointable['fied_old']}')";
                        break;
                    case "Rigth":
                        $jointablestring[] = "->rightJoin('{$jointable['table']}','{$jointable['fied']}','=','{$jointable['fied_old']}')";
                        break;

                    default:
                }

            }
            //这里的关联表
            if(!empty($jointablestring)){
                $jointablestring[] = "->select('{$fieds}')";
                $jointablestring = '$data =$data'.join("\n        ", $jointablestring).';';
            }

            $function_lists=<<<text
 function lists()
    {
        //条件输入
        $where_inputs
        //排序输入
         \$sort =\$this->thisRequest->get('sort');
        \$limit = (int)\$this->thisRequest->get('limit');
       \$limit = \$limit ? \$limit : 15;
        \$data = \$this->thisModel;
        //关联表
        {$jointablestring}
        //条件
        {$where_fieds}
        //排序
        if(\$sort){
             \$sort=(explode(' ',\$sort));
            \$data =\$data->orderBy(\$sort[0],\$sort[1]);
        }

        \$data =\$data ->paginate(\$limit);
        if(\$this->thisRequest->ajax()){

            return commonFuc::data_reture_json(\$data,200,'');
        }


        return view(\$this->tpviewhead . 'lists',['data'=>\$data]);
    }
text;
            return $function_lists;
        }



        /* function lists()
         {
             $limit = (int)$this->thisRequest->get('limit');
             $limit = $limit ? $limit : 15;

             if($this->thisRequest->ajax()){
                 $data = $this->thisModel->paginate($limit);
                 return commonFuc::data_reture_json($data,200,'');
             }
             $data = $this->thisModel
                 ->join()
                 ->paginate($limit);

             return view($this->tpviewhead . 'lists',['data'=>$data]);
         }*/


    }






}
